package com.hutao.sso.service.impl.authority;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.plaf.synth.SynthSeparatorUI;

import org.eclipse.jdt.internal.compiler.batch.Main;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.sso.dataSource.TargetDataSource;
import com.hutao.sso.mapper.newAuthority.AuthoritysMapper;
import com.hutao.sso.pojo.newAuthority.Authoritys;
import com.hutao.sso.service.authority.AuthorityService;
import com.hutao.sso.service.authority.RoleAuthorityService;
import com.hutao.sso.service.authority.RoleService;
import com.hutao.sso.service.authority.UserRolesService;
import com.hutao.sso.util.AuthorityTree;

@Transactional
@Service
public class AuthorityServiceImpl implements AuthorityService{

	@Autowired
	private AuthoritysMapper authoritysMapper;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private RoleAuthorityService roleAuthorityService;
	
	@Autowired
	private UserRolesService userRolesService;

	@TargetDataSource(name = "ds1")
	@Transactional(readOnly = true)
	@Override
	public List<AuthorityTree> findAuthoritysList(Map<String, Object> params) {
		List<Map<String, Object>> authorityParentList = authoritysMapper.findAuthoritysList(params);
		List<AuthorityTree> treeList = new ArrayList<>();
		
		if(null != authorityParentList && authorityParentList.size() > 0) {
			for(int i = 0; i < authorityParentList.size(); i++) {
				AuthorityTree tree = new AuthorityTree();
				tree.setCheckboxValue((String)authorityParentList.get(i).get("id")+"");
				tree.setName((String)authorityParentList.get(i).get("permission_name"));
				tree.setSpread(false);
				System.out.println((String)authorityParentList.get(i).get("roleAuthorityId")+"----------------");
				if((String)authorityParentList.get(i).get("roleAuthorityId") != null) {
					tree.setChecked(true);
				} else {
					tree.setChecked(false);
				}
					
				treeList.add(tree);
		}
		}
		
		return treeList;
	}

//	@TargetDataSource(name = "ds1")
//	@Transactional(readOnly = true)
//	@Override
//	public Map<String,Object> findAuthorityListByRoleId(String userId, String medicalMainstayId) {
//		//查询出所有的角色
//		Map<String, Object> params = new HashMap<>();
//		Map<String,Object> result = new HashMap<>();
//		params.put("userId", userId);
//		params.put("medicalMainstayId", medicalMainstayId);
//		Set<String> authoritySet = new HashSet<>();
//		Set<String> roleSet = new HashSet<>();
//		List<Map<String, Object>> userRoleList = userRolesService.findUserRoleListByUserId(params);
//		if(null != userRoleList && userRoleList.size() > 0) {
//			for(Map<String, Object> map : userRoleList) {
//				String roleId = (String)map.get("role_id");
//				roleSet.add((String)map.get("remark"));
//				//查询某个角色对应的所有权限
//				params.put("roleId", roleId);
//				List<Map<String, Object>> authorityRoleList = authoritysMapper.findAuthoritysByRoleId(params);
//				if(null != authorityRoleList && authorityRoleList.size() > 0) {
//					for(Map<String, Object> authorityRoleMap : authorityRoleList) {
//						authoritySet.add((String)authorityRoleMap.get("session_name"));
//					}
//				}
//				
//			}
//		}
//		result.put("authoritys", authoritySet);
//		result.put("roles", roleSet);
//		
//		
//		return result;
//	}
	
	@TargetDataSource(name = "ds1")
	@Transactional(readOnly = true)
	@Override
	public Set<String> findAuthorityListByRoleId(String userId, String medicalMainstayId) {
		//查询出所有的角色
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("medicalMainstayId", medicalMainstayId);
		Set<String> authoritySet = new HashSet<>();
		Set<String> roleSet = new HashSet<>();
		List<Map<String, Object>> userRoleList = userRolesService.findUserRoleListByUserId(params);
		if(null != userRoleList && userRoleList.size() > 0) {
			for(Map<String, Object> map : userRoleList) {
				String roleId = (String)map.get("role_id");
				roleSet.add((String)map.get("remark"));
				//查询某个角色对应的所有权限
				params.put("roleId", roleId);
				List<Map<String, Object>> authorityRoleList = authoritysMapper.findAuthoritysByRoleId(params);
				if(null != authorityRoleList && authorityRoleList.size() > 0) {
					for(Map<String, Object> authorityRoleMap : authorityRoleList) {
						authoritySet.add((String)authorityRoleMap.get("session_name"));
					}
				}
				
			}
		}
		
		
		return authoritySet;
	}
	
	

}
